/*
 * SystemBuilder.java
 *
 * Created on 8 febbraio 2008, 17.05
 */

package classcreator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  Stefano
 */
public class SystemBuilder extends javax.swing.JFrame implements Showable {
    TypeRepository tr;
    DatatypeRepository dr;
    StateMachineRepository smr;
    double version=0.0;
    
    /** Creates new form SystemBuilder */
    public SystemBuilder() {
        tr=new TypeRepository();
        dr=new DatatypeRepository(tr);
        smr=new StateMachineRepository(tr);
        initComponents();
        showAll();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("0");
        setResizable(false);

        jButton1.setMnemonic('t');
        jButton1.setText("Types");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setMnemonic('d');
        jButton2.setText("Datatypes");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setMnemonic('s');
        jButton3.setText("StateMachines");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Load");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Save");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("BuildSystem");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel1.setText("The fantasmagorical Stefano Gogioso's IS system builder!");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(6, 6, 6)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton6))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5)
                    .addComponent(jButton6)))
        );

        pack();
    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new TypeMaker(tr).setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        new DatatypeMaker(dr,tr).setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        new StateMachineMaker(smr,tr).setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String filename=jTextField1.getText();
        if(filename.isEmpty()) return;
        System.out.println("----------------");
        System.out.println("Starting load...");
        try {
            BufferedReader br = null;
            
            br = new BufferedReader(new FileReader(filename+".pj"));
            version=Double.parseDouble(br.readLine());
            int n;
            n=Integer.parseInt(br.readLine());
            System.out.println("Types on load: "+n);
            for(int i=0; i<n; i++){
                tr.add(Type.parse(br, tr));
                System.out.println("-Loaded Type: "+tr.lastElement().name);
                //System.out.println(tr.get(i+Naming.primitiveTypes.size()).toString());
            }
            n=Integer.parseInt(br.readLine());
            System.out.println("Datatypes on load: "+n);
            for(int i=0; i<n; i++){
                dr.add(Datatype.parse(br, tr));
                System.out.println("-Loaded Datatype: "+dr.lastElement().name);
                //System.out.println(dr.get(i).toString());
            }
            n=Integer.parseInt(br.readLine());
            System.out.println("StateMachines on load: "+n);
            for(int i=0; i<n; i++){
                smr.add(StateMachine.parse(br, tr));
                System.out.println("-Loaded StateMachine: "+smr.lastElement().name);
                //System.out.println(smr.get(i).toString());
            }
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(SystemBuilder.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Load failed!");
        System.out.println("----------------");
        return;
        }
        System.out.println("Load completed succesfully...");
        System.out.println("----------------");
    }

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        String filename=jTextField1.getText();
        if(filename.isEmpty()) return;
        System.out.println("----------------");
        System.out.println("Starting save...");
        try {
            BufferedWriter br = null;
            // br = new BufferedWriter(new FileWriter("IS"));
            br = new BufferedWriter(new FileWriter(filename+".pj"));
            br.write(String.valueOf(version)); br.newLine();
            int n;
            n=tr.size()-Naming.primitiveTypes.size();
            br.write(String.valueOf(n));
            br.newLine();
            for(int i=0; i<n; i++){
                tr.get(i+Naming.primitiveTypes.size()).permanentize(br, tr);
                System.out.println("-Saved Type: "+tr.get(i+Naming.primitiveTypes.size()).name);
            }
            n=dr.size();
            br.write(String.valueOf(n));
            br.newLine();
            for(int i=0; i<n; i++){
                dr.get(i).permanentize(br, tr);
                System.out.println("-Saved Datatype: "+dr.get(i).name);
            }
            n=smr.size();
            br.write(String.valueOf(n));
            br.newLine();
            for(int i=0; i<n; i++){
                smr.get(i).permanentize(br, tr);
                System.out.println("-Saved StateMachine: "+smr.get(i).name);
            }
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(SystemBuilder.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Save failed!");
        System.out.println("----------------");
        return;
        }
        System.out.println("Save completed succesfully...");
        System.out.println("----------------");
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        System.out.println("----------------");
        System.out.println("Starting build...");
        for(Datatype dt:dr){
            try {
                System.out.println("-Building Datatype: "+dt.name);
                BufferedWriter br = null;
                br = new BufferedWriter(new FileWriter(dt.name+".java"));
                br.write(dt.toString());
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(SystemBuilder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        for(StateMachine dt:smr){
            try {
                System.out.println("-Building StateMachine: "+dt.name);
                BufferedWriter br = null;
                br = new BufferedWriter(new FileWriter(dt.name+".java"));
                br.write(dt.toString());
                br.close();
                
                StateMachineTester smt=new StateMachineTester(dt);
                System.out.println("-Building JUnit TestCase: "+smt.getClassName()+" for StateMachine: "+dt.name);
                br = new BufferedWriter(new FileWriter(smt.getClassName()+".java"));
                br.write(smt.toString());
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(SystemBuilder.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Build failed");
            System.out.println("----------------");
            return;
            }
        }
        System.out.println("Build completed succesfully...");
        System.out.println("----------------");
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SystemBuilder().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    public void showAll() {
        this.setVisible(true);
        this.setEnabled(true);
        
    }
    
}
